#!/usr/bin/env python
# _#_ coding:utf-8 _*_

"""
获取一年中每个星期的起始时间
"""
import datetime, os, pexpect
import json
import re

import pika
from app.modules.common.utils import sendMqMessage

from app import create_app

current = datetime.datetime.now()
start = datetime.date(current.year, 1, 1)
last_day = datetime.date(current.year, 12, 31)
isfirst = start.weekday()
last_week = last_day.strftime('%W')

weeks = {}
if isfirst != 0:
    end = datetime.timedelta(7 - start.weekday() - 1)
    weeks[0] = [start, start + end]
start += datetime.timedelta(7 - start.weekday())


def print_date(i):
    days = datetime.timedelta(weeks = i)
    end = start + days  # 每周的开始时间

    if i + 1 == int(last_week):
        weeks[i + 1] = [end, last_day]
    else:
        weeks[i + 1] = [end, end + datetime.timedelta(6)]


def allweeks():
    for i in range(0, int(last_week)):
        print_date(i)
    return weeks


def main(week):
    print_date(int(week))

    for (k, week) in weeks.items():
        num = k + 1 <= 9 and "0" + str(k + 1) or str(k + 1)
        print "%s\t%s\t%s\t" % (str(current.year) + num, str(week[0]), str(week[1]))


if __name__ == '__main__':
    # now = datetime.datetime.now()
    # week = now.strftime('%W')
    # print week
    # main(week)
    # _list = ["a",'b','c']
    # print _list.index('c')
    # if 'c' in _list:
    #     _list.remove('c')
    # print _list

    # connection = pika.BlockingConnection(pika.ConnectionParameters(host = '127.0.0.1'))
    # channel = connection.channel()
    # message = {"type":"ONLINE","map":{"key":'aaaa',"value":2}}
    # channel.basic_publish(exchange = 'matrix-exchange',
    #                       routing_key = 'no-use',
    #                       body = json.dumps(message),
    #                       properties=pika.BasicProperties(
    #                                 content_type="application/json",
    #                                 headers={'__TypeId__': 'com.winit.common.CloudLinkSend.MqMessage'},
    #                                 content_encoding= 'UTF-8',
    #                                 delivery_mode=2,  # make message persistent
    #                       ))
    # connection.close()
    # sendMqMessage('matrix.online', {"type": "ONLINE", "map": {"key": 'aaaa', "value": 2}})
    sendMqMessage('uplan.exchange', {"type": "ONLINE", "map": {"plan_id": '1111'}})

    pass
